import 'package:flutter/material.dart';

class BottomBarDemo1 extends StatefulWidget {
  List<Widget> _bodyView = [BodyView1(), BodyView2(), BodyView3(), BodyView4()];

  @override
  _BottomBarDemo1State createState() => _BottomBarDemo1State();
}

class _BottomBarDemo1State extends State<BottomBarDemo1> {
  int _bottomCurrent = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('底部选项卡'),
      ),
      // -----底部选项卡1-默认
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed, //如果等于长度等于4，用fixed  默认为shifting
        currentIndex: _bottomCurrent,
        items: [
          BottomNavigationBarItem(label: '首页', icon: Icon(Icons.home)),
          BottomNavigationBarItem(label: '联系人', icon: Icon(Icons.phone)),
          BottomNavigationBarItem(label: '联系人', icon: Icon(Icons.phone)),
          BottomNavigationBarItem(label: '联系人', icon: Icon(Icons.phone))
        ],
        onTap: (e) {
          setState(() {
            _bottomCurrent = e;
          });
        },
      ),

      //悬浮按钮的位置

      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          setState(() {
            print('点击悬浮按钮');
          });
        },
      ),
      body: widget._bodyView[_bottomCurrent],
    );
  }
}

class BodyView1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('111'),
    );
  }
}

class BodyView2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('222'),
    );
  }
}

class BodyView3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('333'),
    );
  }
}

class BodyView4 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('444l'),
    );
  }
}
